package com.easygoods.service;

import com.bestvike.stone.core.collection.CollectionUtils;
import com.bestvike.stone.core.lang.BeanUtils;
import com.bestvike.stone.core.time.DateUtils;
import com.bestvike.stone.spring.service.BaseService;
import com.easygoods.dao.AdvertInfoDao;
import com.easygoods.dao.AppBannerDao;
import com.easygoods.entity.AdvertInfo;
import com.easygoods.entity.AppBanner;
import com.easygoods.response.AdvertResponse;
import com.easygoods.response.AppBannerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * Created by 许崇雷 on 2019-03-07.
 */
@Service
public class AdvertService extends BaseService {
    @Autowired
    private AdvertInfoDao advertInfoDao;
    @Autowired
    private AppBannerDao appBannerDao;

    //获取当前活动
    public AdvertResponse getCurrentAdvert() {
        Date now = DateUtils.now();
        final Example example = new Example(AdvertInfo.class);
        example.createCriteria().andLessThanOrEqualTo(AdvertInfo.BEGIN_TIME, now)
                .andGreaterThan(AdvertInfo.END_TIME, now);
        example.orderBy(AdvertInfo.BEGIN_TIME).asc();

        final List<AdvertInfo> advertInfos = this.advertInfoDao.selectByExample(example);
        return CollectionUtils.isEmpty(advertInfos) ? null : BeanUtils.toBean(advertInfos.get(0), AdvertResponse.class);
    }

    //获取banner
    public List<AppBannerResponse> getAppBanner() {
        final Example example = new Example(AppBanner.class);
        example.orderBy(AppBanner.DISPLAY_ORDER).asc();
        final List<AppBanner> advertInfos = this.appBannerDao.selectByExample(example);
        return BeanUtils.toList(advertInfos, AppBannerResponse.class);
    }
}
